//
//  ICRobotListViewController.m
//  ICome
//
//  Created by chenqs on 2017/7/21.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICRobotListViewController.h"
#import "ICRobotDetailViewController.h"
#import "ICContactsNetworkManager.h"
#import "ICRobotModel.h"
#import "ICRobotViewCell.h"
#import "ICHeaderFile.h"

@interface ICRobotListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *robotArr;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation ICRobotListViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"机器人列表";
    self.view.backgroundColor = BACKGROUNDCOLOR;
    
    //初始化列表
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    //_tableView.backgroundColor = BACKGROUNDCOLOR;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
//    _tableView.contentInset = UIEdgeInsetsMake(0, 0, 170., 0);
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    [self reloadData];
}


- (NSMutableArray *)robotArr
{
    if (!_robotArr) {
        _robotArr = [NSMutableArray array];
    }
    return _robotArr;
}

- (void)reloadData
{
    NSDictionary *dict = nil;
    __weak typeof(self)weak_self = self;
    //[self.robotArr removeAllObjects];
    [MBProgressHUD showMessage:@"请稍候..." toView:self.view];
    [[ICContactsNetworkManager sharedInstance] getRobotListWithParam:dict requestCache:^(id responseCache) {
        
        weak_self.robotArr = [NSMutableArray arrayWithArray:responseCache];
        [weak_self.tableView reloadData];
        [MBProgressHUD hideLoadingActivityView:weak_self.view];
    } success:^(id responseObject) {
        
        weak_self.robotArr = [NSMutableArray arrayWithArray:responseObject];
        [weak_self.tableView reloadData];
        [MBProgressHUD hideLoadingActivityView:weak_self.view];
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideLoadingActivityView:weak_self.view];
        [MBProgressHUD showError:@"获取失败~"];
    }];
}

#pragma mrak - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.robotArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cellIdentifier_robot";
    ICRobotViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[ICRobotViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    ICRobotModel *model = self.robotArr[indexPath.row];
    [cell setObject:model];
    [cell setIsLast:(model == [self.robotArr lastObject])?YES:NO];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ICRobotModel *model = self.robotArr[indexPath.row];
    if ([ICTools isObjEmpty:(model)]) {
        [MBProgressHUD showError:@"目前无法为您服务~"];
        return;
    }
    ICRobotDetailViewController *robotDetailViewController = [[ICRobotDetailViewController alloc] init];
    robotDetailViewController.model = model;
    [self.navigationController pushViewController:robotDetailViewController animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [ICRobotViewCell setRobotViewCellHeight];
}
@end
